package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.boot.mapper.CompensateRecordsMapper;
import com.boot.pojo.CompensateRecords;
import com.boot.service.CompensateRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
* @author liyan
* @description 针对表【compensaterecords】的数据库操作Service实现
* @createDate 2025-05-08 10:19:33
*/
@Service
public class CompensateRecordsServiceImpl implements CompensateRecordsService {

    @Autowired
    private CompensateRecordsMapper compensaterecordsMapper;
    @Override
    public void recordCompensation(Long userId, BigDecimal amount, String type) {
        CompensateRecords compensateRecords = new CompensateRecords();
        compensateRecords.setAmount(amount);
        compensateRecords.setUserId(userId);
        compensateRecords.setBusinessType(type);
        compensateRecords.setStatus(0);
        compensateRecords.setCreateTime(LocalDateTime.now());
        compensaterecordsMapper.insert(compensateRecords);
    }

    @Override
    public void removeCompensation(Long userId, BigDecimal amount, String type) {
        LambdaQueryWrapper<CompensateRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompensateRecords::getUserId, userId)
                .eq(CompensateRecords::getBusinessType, type)
                .eq(CompensateRecords::getStatus, amount)
                .eq(CompensateRecords::getStatus, 0);
        compensaterecordsMapper.delete(wrapper);

    }

    @Override
    public CompensateRecords getCompensateRecordsForUpdate(Long id) {
        LambdaQueryWrapper<CompensateRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompensateRecords::getId, id)
                .eq(CompensateRecords::getStatus, 0);
        return compensaterecordsMapper.selectOne(wrapper);
    }

    @Override
    public Boolean updateCompensateRecords(CompensateRecords compensateRecords) {
        LambdaUpdateWrapper<CompensateRecords> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CompensateRecords::getId, compensateRecords.getId())
                .set(CompensateRecords::getStatus, compensateRecords.getStatus());
        int updateRow = compensaterecordsMapper.update(wrapper);
        return updateRow > 0;
    }
}




